//
//  DMCommon.m
//  OrderOnline
//
//  Created by YAPPA_3 on 11/30/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "DMCommon.h"
#import "DataManager.h"




//------------------------------------------------------------------------------
typedef struct tagURLPathType
{
	NSString*		prefix;
	URLPATH_TYPE	type;
} URLPathType;
//------------------------------------------------------------------------------

URLPathType	URLPathTypeData[] =
{
	{ @"http:",    URLPATH_TYPE_HTTP },
	{ @"https:",   URLPATH_TYPE_HTTP },
	{ @"page:",    URLPATH_TYPE_LOCALPAGE },
	{ @"resource:",URLPATH_TYPE_RESOURCE },
	{ @"edition:", URLPATH_TYPE_DETAILHTML },
};

const int URLPathTypeDataNum = sizeof(URLPathTypeData)/sizeof(URLPathTypeData[0]);

//------------------------------------------------------------------------------
NSString* getPrefixFromTypeNo( URLPATH_TYPE typeNo )
{
	for ( int i=0 ; i<URLPathTypeDataNum ; i++ )
	{
		if ( URLPathTypeData[ i ].type == typeNo )
		{
			return( URLPathTypeData[ i ].prefix );
		}
	}
	return( nil );
}

//------------------------------------------------------------------------------
URLPATH_TYPE checkURLPathType( NSString* path )
{
	URLPATH_TYPE	type = URLPATH_TYPE_NORMAL;
	
	for ( int i=0 ; i<URLPathTypeDataNum ; i++ )
	{
		NSString* prefix = URLPathTypeData[i].prefix;
		int	len = [prefix length];
		if (( [path length] > len )
			&& ( [path compare:prefix options:NSCaseInsensitiveSearch range:NSMakeRange(0,len) ] == NSOrderedSame ))
		{
			type = URLPathTypeData[i].type;
			break;
		}
	}
	return( type );
}

//------------------------------------------------------------------------------
NSString* getUnPrefix( NSString* path )
{
	NSString*	prefix = getPrefixFromTypeNo( checkURLPathType(path) );
	NSString*	path2;
	if ( prefix )
	{
		int	len = [prefix length];
		if (( [path length] > len )
			&& ( [path compare:prefix options:NSCaseInsensitiveSearch range:NSMakeRange(0,len) ] == NSOrderedSame ))
		{
			path2 = [NSString stringWithFormat:@"%@", [path substringFromIndex:len] ];
			while ( [path2 hasPrefix:@"/"] )
			{
				path2 = [path2 substringFromIndex:1];
			}
			return path2;
		}
	}
	return( nil );
}


//------------------------------------------------------------------------------
NSString* createAbsolutePath( NSString* path )
{
	int	type = checkURLPathType( path );
	
	if ( type == URLPATH_TYPE_NORMAL )
	{
		NSString* resName = [[NSBundle mainBundle] pathForResource:[path stringByDeletingPathExtension]
														    ofType:[path pathExtension]];
		if ( resName != nil )
		{
			return( [NSString stringWithFormat:@"%@//%@", getPrefixFromTypeNo( URLPATH_TYPE_RESOURCE ), path] );
		}
		
		return( [[DataManager Shared] createServerPath:path] );
	}
	else
	{
		return( path );
	}
}


//------------------------------------------------------------------------------
int getPageNoFromPageLink( NSString* path )
{
	int ret = -1;
	
	NSString* page = getUnPrefix(path);
	
	if( page )
	{
		ret = [page intValue];
	}
	
	return( ret );
}


//------------------------------------------------------------------------------
NSString* getResourceNameFromResourceLink( NSString* path )
{
	return getUnPrefix(path);
}


//------------------------------------------------------------------------------
NSArray* getMidAndPidFromDetailLink( NSString* path )
{
	NSString*	prefix = getPrefixFromTypeNo( URLPATH_TYPE_DETAILHTML );
	
	if ( ! prefix )
		return nil;
	
	int	len = [prefix length];
	if (( [path length] <= len )
		|| ( [path compare:prefix options:NSCaseInsensitiveSearch range:NSMakeRange(0,len) ] != NSOrderedSame ))
		return nil;
	
	NSArray* tmp = [[path substringFromIndex:len] componentsSeparatedByString:@"&"];
	
	
	NSString* mid = nil;
	NSString* pid = nil;
	
	NSString* MID_PREFIX = @"MagazineID=";
	NSString* PID_PREFIX = @"pid=";
	
	for ( NSString* str in tmp )
	{
		NSRange range;
		
		range = [str rangeOfString:MID_PREFIX];
		if ( range.location != NSNotFound )
		{
			mid = [NSString stringWithString:[str substringFromIndex:(range.location + range.length)]];
			continue;
		}
		
		range = [str rangeOfString:PID_PREFIX];
		if ( range.location != NSNotFound )
		{
			pid = [NSString stringWithString:[str substringFromIndex:(range.location + range.length)]];
			continue;
		}
	}
	
	if ( mid && pid )
	{
		return( [NSArray arrayWithObjects:mid, pid, nil] );
	}
	return( nil );
}


//------------------------------------------------------------------------------
NSString* getBaseNameFromProduceId( NSString* pid )
{
	NSArray *items = [pid componentsSeparatedByString:@"."];
	
	int splitIdx;
	if([items count] >= 5)
	{
		splitIdx = 5;
	}
	else
	{
		splitIdx = [items count];
	}
	
	NSRange range = [pid rangeOfString:[items objectAtIndex:(splitIdx-1)]];
	return( [pid substringFromIndex:range.location] );
}



